import Vue from 'vue'
// import VueRouter from 'vue-router'//采用CDNJ引入后可以去掉import
Vue.use(VueRouter)

/* 05.20路由懒加载，
好处就是在首页加载的时候，速度会相比之前大大提升，
而且按需加载可以减少网络资源的开销，
提高页面的响应速度。
但是，缺点是每一个页面会单独生成一个js文件，在进行webpack打包的时候会占用资源*/

/* 路由配置表 */
const routes = [
	/* 匹配所有的空路径,跳转到login页面 */
	{
		path: "/",
		redirect: "/login", //重定向到登录页面
		component: resolve => require(['@/views/login/index'], resolve),
	},
	/* 登录组件 */
	{
		path: "/login",
		hiddren: true,
		name: 'login',
		component: resolve => require(['@/views/login/index'], resolve),
	},
	
	/* home页面组件 */
	{
		path: "/home",
		component: resolve => require(['@/views/home/index'], resolve),
		children: [{
				path: "/index",
				component: resolve => require(['@/views/welcome/index'], resolve),
			},//导入首页路由组件

			{
				path: "/welocme",
				component: resolve => require(['@/views/welcome/index'], resolve),
			},//导入首页路由组件

			{
				path: "/userList",
				component: resolve => require(['@/views/user/index'], resolve),
			},//导入用户列表路由组件

			{
				path: "/userModify",
				component: resolve => require(['@/views/user/userModify/index'], resolve),
			},//导入修改权限路由组件

			{
				path: "/employeeInfo",
				component: resolve => require(['@/views/employee/employeeInfo/index'], resolve),
			},//导入员工档案路由组件

			{
				path: "/employeeSalary",
				component: resolve => require(['@/views/employee/employeeSalary/index'], resolve),
			},//导入员工薪水路由组件

			{
				path: "/employeeModify",
				component: resolve => require(['@/views/employee/emplpoyeeModify/index'], resolve),
			},//导入员工流程审批路由组件

			{
				path: "/workState",
				component: resolve => require(['@/views/attendance/attendanceInfo/index'], resolve),
			},//导入考勤状况路由组件

			{
				path: "/workRecord",
				component: resolve => require(['@/views/attendance/attendRecord/index'], resolve),
			},//导入考勤记录路由组件

			{
				path: "/departmentInfo",
				component: resolve => require(['@/views/department/index'], resolve),
			},//导入部门管理路由组件

			{
				path: "/personalInfo",
				component: resolve => require(['@/views/personal/index'], resolve),
			}, //导入个人空间路由

			{
				path: "/logRecord",
				component: resolve => require(['@/views/logging/index'], resolve),
			},//导入参数记录路由组件

			{
				path: "/recycleUser",
				component: resolve => require(['@/views/recycle/recycleUser/index'], resolve),
			},//导入回收用户路由组件

			{
				path: "/recycleEmployee",
				component: resolve => require(['@/views/recycle/recycleEmployee/index'], resolve),
			},//导入回收员工路由组件
		]
	},
]

const router = new VueRouter({
	routes
})



const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}



//挂载路由导航守卫
router.beforeEach((to, from, next) => {
	//to将要访问
	//from从哪里访问
	//next接着的操作  next（url)可以携带地址，重定向url上
	if (to.path == '/login') return next();
	//获取user实例
	let userFlag = window.sessionStorage.getItem("token"); //取出当前用户信息，如果没有token则重新返回登录页面
	if (!userFlag) return next('/auth/user/login'); //判断取反，如果值为空，则跳转到login页面中
	next(); //符合要求则进行下一步的操作
})




//将路由暴露出去
export default router 
